package com.huawei.lynnbit.tool.cocos.processor.leaf;

import com.huawei.lynnbit.tool.cocos.config.game.GameConfig;
import com.huawei.lynnbit.tool.cocos.config.game.task.Task;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class LeafProcessorFactory {
    private static final Map<String, Class<? extends LeafProcessor>> PROCESSOR_MAP = new HashMap<>();

    static {
        PROCESSOR_MAP.put("cmd", CmdProcessor.class);
        PROCESSOR_MAP.put("copyFile", CopyProcessor.class);
        PROCESSOR_MAP.put("copySnippet", CopyProcessor.class);
        PROCESSOR_MAP.put("replaceFile", ReplaceProcessor.class);
        PROCESSOR_MAP.put("replaceString", ReplaceProcessor.class);
        PROCESSOR_MAP.put("deleteFile", DeleteProcessor.class);
        PROCESSOR_MAP.put("deleteString", DeleteProcessor.class);
    }

    public static LeafProcessor newInstance(Task task, GameConfig gameConfig) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<? extends LeafProcessor> cls = PROCESSOR_MAP.get(task.getType());
        Constructor<? extends LeafProcessor> constructor = cls.getConstructor(Task.class, GameConfig.class);
        return constructor.newInstance(task, gameConfig);
    }
}
